import { CopyControllerBase } from "./CopyControllerBase";
import { KingCopyInfoView } from "../kingCopy/KingCopyInfoView";
import { KingCopyPanel2 } from "../kingCopy/KingCopyPanel2";
import { SelectCopyDropTimeView } from "../crossAryadevaSecretCopy/SelectCopyDropTimeView";
import { DataEvent } from "../../../events/DataEvent";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { CopyUtil } from "../CopyUtil";
import { GameProxy } from "../../../mvc/GameProxy";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SSeqPlayerItem } from "../../../../../Message/Public/SSeqPlayerItem";
import { SDirectionCopyBossList } from "../../../../../Message/Public/SDirectionCopyBossList";
import { KingCopyBossInfo } from "../kingCopy/info/KingCopyBossInfo";
import { BossConfig } from "../../../resource/BossConfig";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { SDirectionCopyInfo } from "../../../../../Message/Public/SDirectionCopyInfo";
import { ClockManager } from "../../../manager/ClockManager";
import { SPlayerDirectionCopyInfo } from "../../../../../Message/Public/SPlayerDirectionCopyInfo";
import { DirectionCopyConfig } from "../../../resource/DirectionCopyConfig";
import { KingCopyItemData } from "../kingCopy/info/KingCopyItemData";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
type int = number;
//class KingCopyController
    
    export  class KingCopyController extends CopyControllerBase
    {
        constructor()
        {
            super();
            this._rewardItems = [];
            return;
        }

        /* internal  */get infoView(): KingCopyInfoView
        {
            if (this._infoView == null) 
            {
                this._infoView = new KingCopyInfoView();
            }
            return this._infoView;
        }

        /* internal  */get kingCopyPanel2(): KingCopyPanel2
        {
            if (this._kingCopyPanel2 == null) 
            {
                this._kingCopyPanel2 = new KingCopyPanel2();
            }
            return this._kingCopyPanel2;
        }

        /* internal  */get rewardView(): SelectCopyDropTimeView
        {
            if (this._rewardView == null) 
            {
                this._rewardView = new SelectCopyDropTimeView();
            }
            return this._rewardView;
        }

        /* internal  */onEnterKingCopyHandler(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            if (this._kingCopyPanel2 && !this._kingCopyPanel2.isHide) 
            {
                this.kingCopyPanel2.hide();
            }
            this.infoView.setTitle(CopyUtil.getCopyName(cache.copy.copyInfo));
            this.infoView.show();
            this.addListeners();
            return;
        }

        /* internal  */onCleanHandler(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            this.infoView.hide();
            this.removeListeners();
            this.dispose();
            return;
        }

        /* internal  */onGetCopyMultiplicityReward(arg1: DataEvent): void
        {
            var loc1=arg1.data["times"] as int;
            GameProxy.copy.getDirectionCopyItem(loc1);
            return;
        }

        /* internal  */onGetRewardSuccessHandler(arg1: DataEvent): void
        {
            if (this._rewardView && !this._rewardView.isHide) 
            {
                this._rewardView.dispose();
            }
            return;
        }

        /* internal  */onLeaveMultiplicityRewardCopy(arg1: DataEvent): void
        {
            GameProxy.copy.leftGroup_async();
            return;
        }

        /* internal  */onEnterDirectionDropCopy(arg1: DataEvent): void
        {
            var loc1=arg1.data.vo;
            if (loc1) 
            {
                GameProxy.copy.enterDirectionDropCopy(loc1);
            }
            return;
        }

        /* internal  */onCopyRewardItemsHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSeqPlayerItem;
            this._rewardItems = loc1.playerItem;
            this.infoView.updataAwardInfo(this._rewardItems);
            if (this._isKillAll) 
            {
                this.rewardView.itemData(this._rewardItems);
            }
            return;
        }

        /* internal  */onCopyBossListHandler(arg1: MessageBlock): void
        {
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc8=null;
            var loc1=arg1.messageBase as SDirectionCopyBossList;
            var loc2=[];
            var loc3="";
            if (loc1) 
            {
                if (cache.copy.copyInfo && cache.copy.copyInfo.copyId == 199) 
                {
                    loc6 = 8;
                    var loc9=0;
                    var loc10=loc1.bossInfo;
                    for (loc4 in loc10) 
                    {
                        if (int(loc1.bossInfo[loc4]) != 1) 
                        {
                            continue;
                        }
                        (loc5 = new KingCopyBossInfo()).bossId = int(loc4);
                        loc5.killNum = int(loc1.bossInfo[loc4]);
                        loc2.push(loc5);
                        ++loc7;
                        loc3 = loc3 + ("_" + loc5.bossName);
                    }
                    loc9 = 0;
                    loc10 = loc1.bossInfo;
                    for (loc4 in loc10) 
                    {
                        loc8 = String(BossConfig.instance.getInfoByName(int(loc4)).name.split("（")[0]);
                        if (loc3.indexOf(loc8) != -1) 
                        {
                            continue;
                        }
                        (loc5 = new KingCopyBossInfo()).bossId = int(loc4);
                        loc5.killNum = int(loc1.bossInfo[loc4]);
                        loc2.push(loc5);
                        loc3 = loc3 + ("_" + loc5.bossName);
                    }
                }
                else 
                {
                    loc9 = 0;
                    loc10 = loc1.bossInfo;
                    for (loc4 in loc10) 
                    {
                        (loc5 = new KingCopyBossInfo()).bossId = int(loc4);
                        loc5.killNum = int(loc1.bossInfo[loc4]);
                        loc2.push(loc5);
                        ++loc6;
                        if (loc5.killNum != 1) 
                        {
                            continue;
                        }
                        ++loc7;
                    }
                }
                loc2.sortOn(["bossId"], Array.NUMERIC);
                this.infoView.bossInfo = loc2;
                if (loc7 == loc6) 
                {
                    this._isKillAll = true;
                    this.infoView.stopTime();
                    this.rewardView.setTitle(CopyUtil.getCopyName(cache.copy.copyInfo));
                    this.rewardView.itemData(this._rewardItems);
                    this.rewardView.multipleOptions = GameDefConfig.instance.getCopyDropTime(2);
                    this.rewardView.showReturnBtn(false);
                    this.rewardView.show();
                }
            }
            return;
        }

        /* internal  */onCopyInfoHandler(arg1: MessageBlock): void
        {
            var loc3=null;
            var loc4=0;
            var loc5=0;
            var loc1=arg1.messageBase as SDirectionCopyInfo;
            var loc2=cache.copy.copyInfo.stayTime;
            if (loc1 && loc1.costDt) 
            {
                loc3 = ClockManager.instance.nowDate;
                loc4 = int((loc1.costDt.time - loc3.time) / 1000);
                loc5 = loc2 - loc4 < 25 ? 0 : loc2 - loc4;
                this.infoView.updateLeftTime(loc4, loc5);
            }
            return;
        }

        /* internal  */onGetDirectionCopyInfo(arg1: DataEvent): void
        {
            GameProxy.copy.getDirectionCopyInfo();
            return;
        }

        /* internal  */showRewardView(arg1: DataEvent=null): void
        {
            var loc1=!(arg1 == null);
            this.rewardView.setTitle(CopyUtil.getCopyName(cache.copy.copyInfo));
            this.rewardView.itemData(this._rewardItems);
            this.rewardView.multipleOptions = GameDefConfig.instance.getCopyDropTime(2);
            this.rewardView.showReturnBtn(loc1);
            this.rewardView.show();
            return;
        }

        /* internal  */onGetDirectionCopyInfoSucceed(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc8=0;
            var loc9=0;
            var loc10=0;
            var loc11=0;
            var loc12=0;
            var loc1=arg1.data.copyInfo as SPlayerDirectionCopyInfo;
            if (loc1) 
            {
                loc2 = [];
                loc5 = "";
                loc6 = loc1.todayFinishCopys.length;
                loc9 = (loc1.lastDirection - 1);
                loc10 = int(cache.role.combatCapabilities);
                loc11 = 0;
                while (loc11 < loc6) 
                {
                    loc5 = loc5 + (loc1.todayFinishCopys[loc11] as string);
                    ++loc11;
                }
                loc12 = 0;
                while (loc12 < 6) 
                {
                    if (loc4 = DirectionCopyConfig.instance.getItemByFloor(loc12 + 1)) 
                    {
                        (loc3 = new KingCopyItemData()).copyCode = loc4.copyCode;
                        loc3.index = loc12;
                        loc3.todayFinishCopys = loc5;
                        loc3.lastFinishCopy = loc1.lastChallengeCopy;
                        loc3.copyGrade = loc1.copyMarkInfo[loc4.copyCode] as int;
                        loc3.copyState = loc1.copyStatusInfo[loc4.copyCode] as int;
                        if (loc3.lastFinishCopy != loc4.copyCode) 
                        {
                            loc7 = -1;
                        }
                        else if (CopyUtil.getCanOpenNextKingCopy(loc12 + 2)) 
                        {
                            loc7 = loc12 + 1;
                            loc8 = loc12 + 1;
                        }
                        else 
                        {
                            loc7 = loc12;
                            loc8 = loc12;
                        }
                        loc3.todayFinishCopysIndex = loc7;
                        loc2.push(loc3);
                    }
                    ++loc12;
                }
            }
            this.kingCopyPanel2.itemData(loc2, loc8, loc9);
            this.kingCopyPanel2.show();
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.EnterKingCopy, this.onEnterKingCopyHandler);
            Dispatcher.addEventListener(EventName.LeaveKingCopy, this.onCleanHandler);
            Dispatcher.addEventListener(EventName.EnterDirectionDropCopy, this.onEnterDirectionDropCopy);
            Dispatcher.addEventListener(EventName.GetDirectionCopyInfo, this.onGetDirectionCopyInfo);
            Dispatcher.addEventListener(EventName.GetDirectionCopyInfoSucceed, this.onGetDirectionCopyInfoSucceed);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDirectionCopyRewardItems, this.onCopyRewardItemsHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDirectionCopyBossList, this.onCopyBossListHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDirectionCopyInfo, this.onCopyInfoHandler);
            return;
        }

        /* internal  */addListeners(): void
        {
            Dispatcher.addEventListener(EventName.GetCopyMultiplicityReward, this.onGetCopyMultiplicityReward);
            Dispatcher.addEventListener(EventName.GetMultiplicityRewardSuccess, this.onGetRewardSuccessHandler);
            Dispatcher.addEventListener(EventName.LeaveMultiplicityRewardCopy, this.onLeaveMultiplicityRewardCopy);
            Dispatcher.addEventListener(EventName.LeaveKingCopyAndShowRewardView, this.showRewardView);
            return;
        }

        /* internal  */removeListeners(): void
        {
            Dispatcher.removeEventListener(EventName.GetCopyMultiplicityReward, this.onGetCopyMultiplicityReward);
            Dispatcher.removeEventListener(EventName.GetMultiplicityRewardSuccess, this.onGetRewardSuccessHandler);
            Dispatcher.removeEventListener(EventName.LeaveMultiplicityRewardCopy, this.onLeaveMultiplicityRewardCopy);
            Dispatcher.removeEventListener(EventName.LeaveKingCopyAndShowRewardView, this.showRewardView);
            return;
        }

        public get rewardItemsNum(): int
        {
            return this._rewardItems.length;
        }

        public /* override */ stageResize(): void
        {
            if (this._infoView) 
            {
                this._infoView.updatePosition();
            }
            return;
        }

        public /* override */ dispose(): void
        {
            if (this._infoView) 
            {
                this._infoView.clean();
                this._infoView = null;
            }
            if (this._rewardView) 
            {
                this._rewardView.dispose();
                this._rewardView = null;
            }
            this._rewardItems = [];
            this._isKillAll = false;
            return;
        }

        /* internal */ /* var */_infoView: KingCopyInfoView;

        /* internal */ /* var */_rewardView: SelectCopyDropTimeView;

        /* internal */ /* var */_kingCopyPanel2: KingCopyPanel2;

        /* internal */ /* var */_rewardItems: Array<any>;

        /* internal */ /* var */_isKillAll: Boolean;
    }
